#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
作  者    : 北极星光 light22@126.com
创建时间  : 2023-09-05 18:15:54
最后编辑  : 2023-09-08 11:33:03
编辑人    : 北极星光
'''

# 文档地址：https://tts.acgnai.top/docs

import requests
import yaml


def genshin_voice(model_name, text):
    '''
    model_name: 模型名称
    text: 转语音的文字内容
    '''
    with open('config/genshin_voice_config.yaml', 'r', encoding='utf-8') as f:
        cfg = yaml.load(f, Loader=yaml.FullLoader)
    url = 'https://gsv2p.acgnai.top/infer_single'

    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {cfg.get("api_key")}'
    }

    payload = {
        "version": cfg.get("version"),
        "model_name": f"{model_name}",
        "prompt_text_lang": cfg.get("prompt_text_lang"),
        "emotion": cfg.get("emotion"),
        "text": f"{text}",
        "text_lang": cfg.get("text_lang"),
        "top_k": cfg.get("top_k"),
        "top_p": cfg.get("top_p"),
        "temperature": cfg.get("temperature"),
        "text_split_method": cfg.get("text_split_method"),
        "batch_size": cfg.get("batch_size"),
        "batch_threshold": cfg.get("batch_threshold"),
        "split_bucket": cfg.get("split_bucket"),
        "speed_facter": cfg.get("speed_facter"),
        "fragment_interval": cfg.get("fragment_interval"),
        "media_type": cfg.get("media_type"),
        "parallel_infer": cfg.get("parallel_infer"),
        "repetition_penalty": cfg.get("repetition_penalty"),
        "seed": cfg.get("seed"),
        "sample_steps": cfg.get("sample_steps"),
        "if_sr": cfg.get("if_sr")
    }

    res = requests.post(url=url, headers=headers, json=payload)

    return res.json()

    # with open('output/output.wav', 'wb') as f:
    #     f.write(res.content)


if __name__ == '__main__':
    model_name = '原神-中文-宵宫_ZH'
    text = '啊————,啊————,好舒服！快点！————老公~~，再快点儿~~'
    data = genshin_voice(model_name=model_name, text=text)
    print(data)
